#include "recboxitem.h"
#include "ui_recboxitem.h"

RecBoxItem::RecBoxItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecBoxItem)
{
    ui->setupUi(this);

    //一定要记得安装时间过滤器
    ui->musicImageBox->installEventFilter(this);
}

RecBoxItem::~RecBoxItem()
{
    delete ui;
}

bool RecBoxItem::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == ui->musicImageBox)
    {
        if(event->type() == QEvent::Enter)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation->setDuration(100);
            animation->setStartValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            connect(animation, &QPropertyAnimation::finished, this, [=]()
            {
                delete animation;
            });
            return true;
        }
        else if(event->type() == QEvent::Leave)
        {
            QPropertyAnimation* animation = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation->setDuration(100);
            animation->setStartValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->setEndValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation->start();

            connect(animation, &QPropertyAnimation::finished, this, [=]()
            {
                delete animation;
            });
        }
        return true;
    }
    return QObject::eventFilter(watched, event);
}

void RecBoxItem::setText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void RecBoxItem::setImage(const QString &imagePath)
{
    QString style = "#musicImageBox { background-image: url(" + imagePath + "); }";
    ui->musicImageBox->setStyleSheet(style);
}
